package com.darren.archive.interceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Slf4j
public class TokenInterceptor implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception {

        String requestUri = request.getRequestURI();
        String projectName = request.getContextPath();
        System.out.println("preHandle start");
        //将开始时间添加到requestWebConfig
        request.setAttribute("start",new Date().getTime());
        if(handler instanceof  HandlerMethod){
            HandlerMethod object=(HandlerMethod)handler;
            //被过滤的方法
            Method method=object.getMethod();

            //被过滤的对象
            Object  bean = object.getBean();
            System.out.println(method.getName());
            System.out.println(bean.getClass());
        }else{
            log.info("requestUri:"+requestUri);
            log.info("projectName:"+projectName);
        }

        return true;
    }

    @Override
    public void  postHandle(HttpServletRequest request,HttpServletResponse response,Object handler,ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handler,Exception  ex) throws Exception {

    }
}
